package com.chixing.day20_annotation_jdk8.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MyMessageProcessor {
    public static void main(String[] args) {

        try {
            //加载annotationTest.class类
            Class clazz = MyMessageProcessor.class.getClassLoader().
                    loadClass("com.chixing.day20_annotation_jdk8.annotation.AnnotationTest");

            //获取属性
            Field[] fields = clazz.getDeclaredFields();
            //遍历属性
            for (Field field: fields){
                MyMessage myMessage = field.getAnnotation(MyMessage.class);
                System.out.println("name:"+myMessage.name()+" num:"+myMessage.num()
                +" desc:"+myMessage.desc());
            }
            //获取类中的方法
            Method[] methods = clazz.getMethods();
            //遍历方法
            for (Method method:methods){
                //判断方法是否带有MyMessage注解
                if (method.isAnnotationPresent(MyMessage.class)){
                    //获取所有注解method.getDeclaredAnnotations()
                    //获取MyMessage注解
                    MyMessage myMessage = method.getAnnotation(MyMessage.class);
                    System.out.println("name:"+myMessage.name()+" num:"+myMessage.num()
                            +" desc:"+myMessage.desc());
                }
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
}
